package com.easylinkin.linkappapi.machinery.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.machinery.dto.MachineryUserLinkDTO;
import com.easylinkin.linkappapi.machinery.entity.MachineryUserLink;
import com.easylinkin.linkappapi.machinery.service.MachineryUserLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 人员类别关联管理
 */
@RestController
@RequestMapping("machineryUserLink")
@Api(tags = "人员类别关联管理")
public class MachineryUserLinkController {
  
  @Autowired
  private MachineryUserLinkService machineryUserLinkService;

  /**
   * @Description: 增加人员类别关联
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping
  @ApiOperation("增加人员类别关联")
  public RestMessage insert(@RequestBody MachineryUserLinkDTO machineryUserLinkDTO){
    //参数验证
    Assert.notNull(machineryUserLinkDTO.getTypeId(),"类别不能为空");
    Assert.notNull(machineryUserLinkDTO.getUserId(),"人员不能为空");
    machineryUserLinkService.insert(machineryUserLinkDTO);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 删除人员类别关联（包含批量删除）
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping
  @ApiOperation("删除人员类别关联（包含批量删除）")
  public RestMessage delBatch(@RequestBody MachineryUserLink machineryUserLink){
    Assert.notEmpty(machineryUserLink.getIds(),"id不能为空");
    machineryUserLinkService.removeByIds(machineryUserLink.getIds());
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 编辑人员类别关联
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping
  @ApiOperation("编辑人员类别关联")
  public RestMessage updateById(@RequestBody MachineryUserLinkDTO machineryUserLinkDTO){
    Assert.notNull(machineryUserLinkDTO.getId(),"id不能为空");
    machineryUserLinkService.updateOne(machineryUserLinkDTO);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据id查询人员类别关联详情
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("根据id查询人员类别关联详情")
  public RestMessage findById(@PathVariable("id")Integer id) {
    Assert.notNull(id,"id不能为空");
    MachineryUserLink machineryUserLink = machineryUserLinkService.getById(id);
    MachineryUserLinkDTO machineryUserLinkDTO = BeanUtil
        .toBean(machineryUserLink, MachineryUserLinkDTO.class);
    String certificateId = machineryUserLink.getCertificateId();
    if (StringUtils.isNotBlank(certificateId)){
      machineryUserLinkDTO.setCertificateIds(Arrays.asList(certificateId.split(",")));
    }
    return RestBuilders.successBuilder().data(machineryUserLinkDTO).build();
  }

  /**
   * @Description: 根据条件，分页(不分页)查询
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，分页(不分页)查询")
  public RestMessage queryListByPage(@RequestBody RequestModel<MachineryUserLinkDTO> requestModel){
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    IPage<MachineryUserLinkDTO> record =  machineryUserLinkService.queryListByPage(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }
}

